package com.holystone.orca.util;

import java.util.*;

public class TacticsUtil {
	/**
	 * 根据所有的执行时间生成所需要的cron集合
	 * @param execDate 执行日期
	 * @param startTime 开始时间（一天中）
	 * @param endTime 结束时间（一天中）
	 * @param intervalTime 间隔时间
	 * @return List Cron
	 */
	public List<String> getCron(String execDate,String startTime,String endTime,String intervalTime){
		List<String> cronList = new ArrayList<>();
		if (execDate != null && !"".equals(execDate)) {
			String[] days = execDate.split(",");
			List<String> allDate = new ArrayList<>();
			for (String day : days) {
				allDate.add(day);
			}
			Map<Integer, List<String>> monthMap = new HashMap<>();
			for (int j = 1; j < 13; j++) {
				monthMap.put(j, new ArrayList<>());
			}
			for (int i = 1; i < 13; i++) {
				for (String everyDay : allDate) {
					String[] dayEdit = everyDay.split("-");
					Integer month = Integer.parseInt(dayEdit[1]);
					if (month == i) {
						monthMap.get(i).add(everyDay);
					}
				}
				List<String> oneMonth = monthMap.get(i);
				if (oneMonth != null && oneMonth.size() != 0) {
					cronList.add(getOneCron(oneMonth,startTime, endTime,intervalTime));
				}
			}
		}else{
			String result="";
			if(startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
				startTime = startTime.split(":")[0];
				endTime = endTime.split(":")[0];
				if(startTime.equals(endTime)){
					result = "0 0/"+intervalTime+" * * * ? *";
				}else{
					int endTimei = Integer.parseInt(endTime)-1;
					if(Integer.parseInt(startTime) == endTimei){
						result = "0 0/"+intervalTime+" "+startTime+" * * ? *";
					}else{
						result = "0 0/"+intervalTime+" "+startTime+"-"+endTimei+" * * ? *";
					}
				}
			}
			cronList.add(result);
		}
		return cronList;
	}
	/**
	 * 根据所有的执行时间生成所需要的cron集合
	 * @param execDate 执行日期
	 * @param intervalTime 间隔时间
	 * @return List Cron
	 */
	public List<String> getCron(String execDate,String intervalTime){
		return getCron(execDate,null,null,intervalTime);
	}

	/**
	 * 获得Cron
	 * @param interval 间隔时间
	 * @return cron
	 */
	public String getCron(String interval){
		return "0 0/"+interval+" * * * ? *";
	}

	/**
	 * 描述：该方法能够根据参数返回采集cron策略字符串
	 * 
	 * @param dates List 时间集合
	 * @param starttime
	 * @param endtime
	 * @param intervalTime 间隔
	 * @return
	 */
	private String getOneCron(List<String> dates, String starttime, String endtime, String intervalTime) {
		String cron = "";
		String days = "";
		String hour = "";
		String minute = "";
		String month = dates.get(0).split("-")[1];
		GregorianCalendar gc = new GregorianCalendar();
		gc.set(Calendar.MONTH, Integer.valueOf(month)-1);
		int maxDay = gc.getActualMaximum(Calendar.DAY_OF_MONTH);
		if(dates.size()==maxDay){
			days = "*";
		}else{
			for(String d :dates){
				days = days+d.split("-")[2]+",";
			}
			if(days.length()>0)
				days=days.substring(0,days.length()-1);
		}
		if(starttime!=null&&!starttime.equals("")){
			String[] startArr = starttime.split(":");
			if(endtime!=null&&!endtime.equals("")){
				String[] endArr = endtime.split(":");
				if(endArr[0].equals("00")){
					endArr[0] = "23";
				}else{
					endArr[0] = Integer.valueOf(endArr[0])-1<10?"0"+(Integer.valueOf(endArr[0])-1):""+(Integer.valueOf(endArr[0])-1);
				}
				if(startArr[0].equals(endArr[0])){
					hour = startArr[0];
				}else{
					hour = startArr[0]+"-"+ endArr[0];
				}
				minute = "0/"+intervalTime;
				cron = "0 "+minute+" "+hour+" "+days+" " +month+" ?";
			}else{
				cron = "0 0 "+(startArr[0].startsWith("0")?startArr[0].substring(1):startArr[0])+" "+days+" " +month+" ?";
			}
		}else{
			cron = "0 0/"+intervalTime+" * "+days+" " +month+" ?";
		}
		return cron;
	}
	
	public static void main(String[] args) {
		TacticsUtil data = new TacticsUtil();
		List<String> dates = new ArrayList<String>();
//		dates.add("2015-04-13");
//		dates.add("2015-04-14");
//		dates.add("2015-04-15");
//		dates.add("2015-04-16");
		String sdate = "00:00:00";
		String edate = "00:00:00";
		//String edate = "23:00:00";
		String interval = "5";
		System.out.println(data.getOneCron(dates,sdate,edate,interval));
	}
}
